//
//  shareViewController.m
//  Wtracker
//
//  Created by Anna on 7/5/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "shareViewController.h"
#import "WtrackerAppDelegate.h"

#define DEFAULTEMAIL @"Preferred default email"
#define YOURNAME @"Your Name"
#define YOUREMAIL @"Your Email"
#define UOM @"Unit of Measure"

@implementation shareViewController

@synthesize weights, myDict, tmpBodyStr, unitArray;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
	WtrackerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	weights = appDelegate.weights;
	myDict = appDelegate.myDict;
	NSLog(@"in svc");

 }



#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [weights count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
	
	NSArray *tmp1 = [[NSArray alloc] initWithArray:[weights objectAtIndex:indexPath.row]];
	NSString *myUOM = [myDict objectForKey:UOM];
	NSNumber *idx = [NSNumber numberWithInt:[unitArray indexOfObject:myUOM]];
	float weight = [[tmp1 objectAtIndex:([idx intValue]+1)] doubleValue];
	
	NSString *weightStr = [NSString stringWithFormat:@"%.1f", weight];
	
	NSString *thedate = [tmp1 objectAtIndex:3];
	NSString *rowStr = [NSString stringWithFormat:@"%@,  %@ %@", thedate, weightStr, myUOM ];
	
	cell.textLabel.text = rowStr;    
    return cell;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

	NSString *to_email = [myDict objectForKey:DEFAULTEMAIL];
	
	UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	NSString *cellStr = cell.textLabel.text;
	
	NSString *cellStrEsc = [cellStr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

	NSString *myName = [myDict objectForKey:YOURNAME];
	NSString *myEmail = [myDict objectForKey:YOUREMAIL];
	
	NSString *subjStr = [NSString stringWithFormat:@"Weight Log From %@", myName];
	NSString *subjStrEsc = [subjStr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
							
	NSString *bodyStr = [NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@",to_email,myEmail,subjStrEsc,cellStrEsc];
	NSLog(@"bodyStrEsc: %@", bodyStr);

	[self showInfoAlert];
	
	NSURL *mailURL = [NSURL URLWithString:bodyStr];
	BOOL success = [[UIApplication sharedApplication] openURL:mailURL];
	if(success){
		NSLog(@"mail successfully launched");
	}else {
		NSLog(@"mail failed. mailURL: %@", mailURL);
	}
}

-(void)showInfoAlert{
	UIAlertView *infoAlert = [[[UIAlertView alloc] initWithTitle:@"Email" message:@"Click OK to send email. This will exit the Wtracker app." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: @"OK",nil] autorelease];
	[infoAlert show];
	
}



#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

